package com.im.service.noticeManage.smo;

import com.im.MDA;
import com.im.service.common.dao.IFileOperDAO;
import com.im.service.common.dao.IFileQueryDAO;
import com.im.service.contract.dao.IContractOperDAO;
import com.im.service.dto.Attachment;
import com.im.service.dto.Contract;
import com.im.service.dto.FileUpload;
import com.im.service.dto.NoticeManage;
import com.im.service.dto.Result;
import com.im.service.model.NoticeManageVo;
import com.im.service.noticeManage.dao.INoticeManageOperDao;
import com.im.service.noticeManage.dao.INoticeManageQueryDao;
import com.im.service.result.smo.IResultOperSMO;
import com.im.service.result.smo.IResultQuerySMO;
import com.im.util.ListUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class NoticeManageOperSmoImpl implements INoticeManageOperSmo {

    @Autowired
    private INoticeManageOperDao noticeManageOperDao;
    @Autowired
    private INoticeManageQueryDao noticeManageQueryDao;
    @Autowired
    private IResultOperSMO resultOperSMO;
    @Autowired
    private IResultQuerySMO resultQuerySMO;
    @Autowired
    private IFileOperDAO fileOperDAO;
    @Autowired
    private IFileQueryDAO fileQueryDAO;
	@Autowired
	private IContractOperDAO contractOperDAO;

    @Override
    public void addNoticeManage(NoticeManage noticeManage) throws Exception{
        noticeManageOperDao.addNoticeManage(noticeManage);
    }

    @Override
    public void updateNoticeManage(NoticeManageVo noticeManageVo) throws Exception{
        String orgId = noticeManageVo.getOrgId();
        BigDecimal money = noticeManageVo.getMoney();
        noticeManageOperDao.updateNoticeManage(noticeManageVo);

        NoticeManage noticeManageParam = new NoticeManage();
        noticeManageParam.setNoticeManageId(noticeManageVo.getNoticeManageId());
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("noticeManage", noticeManageParam);
        NoticeManage noticeManage = noticeManageQueryDao.queryNoticeManageDetail(param);
        if(!(null==orgId && null==money)){
            if(null!=noticeManage){
                Result result = new Result();
                result.setResultId(noticeManage.getResultId());
                if(null!=orgId){
                    result.setOrgId(orgId);
                }
                if(null!=money){
                    result.setMoney(money);
                }
                resultOperSMO.updateResult(result);
            }
        }

        //处理招标文件内容
        //新增
        List<Attachment> fileList = noticeManageVo.getFileList();
        if(!ListUtil.isListEmpty(fileList)){
            Result r = resultQuerySMO.queryResultById(noticeManage.getResultId());
            for(Attachment attachment : fileList){
                FileUpload fileUpload = new FileUpload();
                String uploadType = "";
                if (attachment.getAttachSuffix() != null && attachment.getAttachSuffix().length() > 0) {
                    uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(attachment.getAttachSuffix().toLowerCase());
                }
                String uploadId = UUID.randomUUID().toString();
                fileUpload.setUploadId(uploadId);
                fileUpload.setUploadObjType("90");
                fileUpload.setUploadObjId(r.getTenderInfoId());
                fileUpload.setUploadType(uploadType);
                fileUpload.setUploadName(attachment.getAttachFullName());
                fileUpload.setUploadSize(attachment.getAttachSize());
                fileUpload.setUploadDate(attachment.getUploadDt());
                fileUpload.setFilePath(attachment.getAttachPath());
                fileUpload.setFileSuffix(attachment.getAttachSuffix());
                fileOperDAO.saveFileUpload(fileUpload);
            }
        }
        //删除
        List<Attachment> delFiles = noticeManageVo.getFileList();
        if(!ListUtil.isListEmpty(delFiles)){
            for(Attachment attachment : delFiles){
                FileUpload fileUpload = new FileUpload();
                fileUpload.setUploadId(attachment.getUploadId());
                fileOperDAO.delFileUpload(fileUpload);
            }
        }

        //处理通知书文件
        Attachment noticeFile = noticeManageVo.getNoticeFile();
        if(null!=noticeFile){
            FileUpload fileUpload = new FileUpload();
            String uploadType = "";
            if (noticeFile.getAttachSuffix() != null && noticeFile.getAttachSuffix().length() > 0) {
                uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(noticeFile.getAttachSuffix().toLowerCase());
            }
            fileUpload.setUploadId(noticeFile.getUploadId());
            fileUpload.setUploadObjId(noticeManageVo.getNoticeManageId());
            fileUpload.setUploadType(uploadType);
            fileUpload.setUploadName(noticeFile.getAttachFullName());
            fileUpload.setUploadSize(noticeFile.getAttachSize());
            fileUpload.setUploadDate(noticeFile.getUploadDt());
            fileUpload.setFilePath(noticeFile.getAttachPath());
            fileUpload.setFileSuffix(noticeFile.getAttachSuffix());
            fileOperDAO.saveFileUpload(fileUpload);
        }
        
      //修改合同状态
      String contractId = noticeManageVo.getContractId();
      String contractStatusCd = noticeManageVo.getContractStatusCd();
      if(contractId !=null && !"".equals(contractId)){
    	  Contract contract = new Contract();
          contract.setContractId(contractId);
          contract.setStatusCd(contractStatusCd);
          contractOperDAO.updateContract(contract);
      }
    }

    @Override
    public void updateNoticeManageByBatch(NoticeManageVo noticeManageVo) throws Exception{
        List<String> noticeManageIds = noticeManageVo.getNoticeManageIds();
        String statusCd = noticeManageVo.getStatusCd();
        if(!ListUtil.isListEmpty(noticeManageIds)){
            for(String noticeManageId:noticeManageIds){
                NoticeManage noticeManageParam = new NoticeManage();
                noticeManageParam.setNoticeManageId(noticeManageId);
                Map<String,Object> param = new HashMap<String,Object>();
                param.put("noticeManage", noticeManageParam);
                NoticeManage noticeManage = noticeManageQueryDao.queryNoticeManageDetail(param);
                if(null!=noticeManage){
                    NoticeManage newNoticeManage = new NoticeManage();
                    newNoticeManage.setNoticeManageId(noticeManage.getNoticeManageId());
                    if(!StringUtils.isEmpty(statusCd)){
                        newNoticeManage.setStatusCd(statusCd);
                        noticeManageOperDao.updateNoticeManage(newNoticeManage);
                    }
                }
            }
        }
    }

    @Override
    public void deleteNoticeManage(NoticeManageVo noticeManageVo) throws Exception{
        NoticeManage noticeManageParam = new NoticeManage();
        noticeManageParam.setNoticeManageId(noticeManageVo.getNoticeManageId());
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("noticeManage", noticeManageParam);
        NoticeManage noticeManage = noticeManageQueryDao.queryNoticeManageDetail(param);
        if(null!=noticeManage){
            //1.删除中标通知书
            noticeManageOperDao.deleteNoticeManage(noticeManageVo);
            //2.更新中标结果表的中标单位和中标金额为空
//            Result result = new Result();
//            result.setResultId(noticeManage.getResultId());
//            result.setOrgId("");
//            result.setMoney(new BigDecimal("0.00"));
//            resultOperSMO.updateResult(result);

            //3.处理中标通知书文档信息
            //处理招标文件内容
            //删除
            Result r = resultQuerySMO.queryResultById(noticeManage.getResultId());
            if(null!=r){
                FileUpload fileUpload = new FileUpload();
                fileUpload.setUploadObjType("90");
                fileUpload.setUploadObjId(r.getTenderInfoId());
                fileOperDAO.delFileUpload(fileUpload);
            }

            //处理通知书文件
            FileUpload fileUpload = new FileUpload();
            fileUpload.setUploadObjId(noticeManageVo.getNoticeManageId());
            fileOperDAO.delFileUpload(fileUpload);
        }
    }
}
